﻿#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h> 
#include <stdlib.h>
 
#include <sys/ioctl.h>
 
#include <map>
#include <vector>

#include "fore_rtdb.h"
#include "base.h"

#include "rtdbFun.h"
  
using namespace std; 

unsigned char    CMD_ACTION_UPDATE_DATA   = 100; 
unsigned char    CMD_ACTION_PRINTF        = 101;
unsigned char    CMD_ACTION_GET_RECSIZE   = 102;

class CAnalyzeCmdLine
{
public:
	CAnalyzeCmdLine()
	{
		memset(m_chrNodeId, 0, 64);
		memset(m_fieldname, 0, 64);
		//memset(m_strActionDetail, 0, 64);
		memset(m_val, 0, 32);
		m_action  = 0;
	}
public:
	unsigned char m_action ;
	int	 m_libNo;
	char     m_chrNodeId[64];
	char     m_fieldname[64];
	//char     m_strActionDetail[128];
	char	 m_val[32];

public:

	bool AnalyzeCmdLine( char * pCmdLine ) 
	{
	 
		if( pCmdLine == NULL || strlen( pCmdLine ) == 0 ) return true ;

		bool bResultOfAnalyze = true  ;

		char buff[1024 ] ;
		strcpy( buff, pCmdLine ) ;

		char * pItem = strtok( buff, " \t\r\n" ) ;
	 	
		while( pItem ){
			//printf( "pItem =%s ", pItem );

			if( strcmp( pItem , "-lib" ) == 0 ){ 
				if( m_action != 0 ) {  
					m_action = 0 ;
					bResultOfAnalyze = false;
					break; 
				}  
				pItem = strtok( NULL, " \t\r\n" ) ; 
				if( !pItem ){
					printf( "pItem = NULL \n" );
					m_action = 0 ;
					bResultOfAnalyze = false;
					break;
				}
				//printf( " %s \n", pItem );
				m_libNo = atoi(pItem) ;
			}  
			else if( strcmp( pItem , "-update" ) == 0 ){
				if( m_action != 0 ) {  
					m_action = 0 ;
					bResultOfAnalyze = false;
					break; 
				}
				m_action = CMD_ACTION_UPDATE_DATA ;
				printf( " \n"  );
				/*pItem = strtok( NULL, " \t\r\n" ) ;
				if( !pItem ){
					m_action = 0 ;
					bResultOfAnalyze = false;
					break;
				}*/
				//m_strActionDetail = pItem ;
			}  
			else if( strcmp( pItem , "-node" ) == 0 ){ 
				pItem = strtok( NULL, " \t\r\n" ) ;
				if( !pItem ) {
					m_action = 0 ;
					bResultOfAnalyze = false;
					break;
				}
				strcpy(m_chrNodeId, pItem) ;
				//printf( " %s \n", m_chrNodeId );
			}
			else if( strcmp( pItem , "-field" ) == 0 ){ 
				pItem = strtok( NULL, " \t\r\n" ) ;
				if( !pItem ) {
					m_action = 0 ;
					bResultOfAnalyze = false;
					break;
				}
				strcpy(m_fieldname, pItem) ;
				//printf( " %s \n", m_fieldname );
			} 
			else if( strcmp( pItem , "-val" ) == 0 ){ 
				pItem = strtok( NULL, " \t\r\n" ) ;
				if( !pItem ) { 
					m_action = 0 ;
					bResultOfAnalyze = false;
					break;
				}
				strcpy(m_val, pItem) ;
				//printf( " %s \n", m_val );
			}
			else if( strcmp( pItem , "-recsize" ) == 0 ){
				if( m_action != 0 ) {  
					m_action = 0 ;
					bResultOfAnalyze = false;
					break; 
				}
				m_action = CMD_ACTION_GET_RECSIZE ;
				//printf( " \n"  ); 
			}  
			else{ 
				m_action = 0 ;
				bResultOfAnalyze = false;
				break;
			}
			pItem = strtok( NULL, " \t\r\n" ) ;
		}
		  

		return bResultOfAnalyze;
	}
	
private:
	
	//char  m_strActionDetail[ 128] ;
};



 
int MY_PRINTF_TAG = 1;
int main(int argc, char *argv[])
{ 

	printf( "\n--------rtdb_printf enter!!!\n" );
  	   
	OpenDbms();
 
	void printf_yc_rtdb( ); 
	void get_rtdb_size(int lib_no);
	void printf_param_rtdb();  
	void printf_dev_rtdb();
	void printf_yk_rtdb();
	void printf_devtype();
	void printf_meatemplate(); 
	void remove_yc_double_name();
	void remove_dev_double_name();
	void printf_yx_rtdb( );
	void printf_event_rtdb();
	void printf_curve_rtdb();

	void update_yc_rtdb(CAnalyzeCmdLine & cmd);
	void update_yx_rtdb(CAnalyzeCmdLine & cmd);

	if( argc >= 2 ) { 
		//int lib_no  = atoi(argv[1]); 

		string   cmdLine ;
		
		for( int i=1; i<argc; i++ )
		{ 
			char tmp[64]={0};
			sprintf(tmp, " %s ",  argv[i]);

			cmdLine += tmp;
		}

		printf( "cmd=%s \n", cmdLine.c_str());

		CAnalyzeCmdLine   cmd;
		if( !cmd.AnalyzeCmdLine( (char*)cmdLine.c_str() ) ) {
			printf( "**************************************************************************** \n" );
			printf( "**** -lib x 					     			**** \n" );  
			printf( "**** -lib x -recsize 				     			**** \n" );
			printf( "**************************************************************************** \n" );
			return 0;
		}

		
 		if( cmd.m_action == CMD_ACTION_GET_RECSIZE )
		{ 
			get_rtdb_size(cmd.m_libNo); 
			return 0;
		}
		
		switch(cmd.m_libNo)
		{	 
			case FORE_DEV_INFO_NO:
				printf_dev_rtdb();
				break;
			case (1000+FORE_DEV_INFO_NO):
				remove_dev_double_name();
				break;
			case FORE_YC_POINT_NO:
				{
					if( cmd.m_action == CMD_ACTION_UPDATE_DATA ) {
						update_yc_rtdb(cmd);
					}
					else {
						 
						   printf_yc_rtdb( );
					}
				}
				break;
			case (1000+FORE_YC_POINT_NO):
				remove_yc_double_name();
				break;
			case FORE_YX_POINT_NO:
				if( cmd.m_action == CMD_ACTION_UPDATE_DATA ) {
					update_yx_rtdb(cmd);
				} 
				else 
					printf_yx_rtdb( );
				break;
			case FORE_YK_NO:
				printf_yk_rtdb();
				break;
			case FORE_DEV_TYPE_NO:
				printf_devtype();
				break;
			case FORE_MEA_TEMPLATE_NO:
				printf_meatemplate();
				break; 
			case FORE_PARAM_CFG_NO:
				printf_param_rtdb();
				break;  
			case FORE_EVENT_POINT_NO:
				printf_event_rtdb();
				break;    
			case FORE_CURVE_NO:
				printf_curve_rtdb();
				break;    
			default: 
			{
				printf( "\n--------invalid lib_no=%d...\n", cmd.m_libNo );
				printf( "\n--------valid lib_no[FORE_DEV_INFO=%d, YC_POINT=%d,YX_POINT=%d, FORE_YK=%d, PARAM_CFG=%d, FORE_DEV_TYPE=%d, FORE_MEA_TEMPLATE=%d FORE_EVENT_POINT=%d FORE_CURVE_NO=%d ]...\n", FORE_DEV_INFO_NO, FORE_YC_POINT_NO, FORE_YX_POINT_NO,  FORE_YK_NO, FORE_PARAM_CFG_NO, FORE_DEV_TYPE_NO, FORE_MEA_TEMPLATE_NO, FORE_EVENT_POINT_NO, FORE_CURVE_NO  );
				break;
			}
		}
	} 
	else {  
			printf( "**************************************************************************** \n" );
			printf( "**** -lib x 					     			**** \n" );  
			printf( "**** -lib x -recsize 				     			**** \n" );
			printf( "**************************************************************************** \n" );
	}
		  

	return 0;
}


void get_rtdb_size(int lib_no)
{  
	if( !IsLibNoValid( lib_no ) ) {
		printf("libNo=%d is InValid \n", lib_no);
		return ;
	}
	int lib_size = GetLibSize(lib_no);
	int rec_size = GetRecCount(lib_no);
	int completeWidth = GetCompleteRecWidth(lib_no);

	printf("libNo=%d : LibSize=%d,  RecSize=%d,  completeWidth=%d \n", lib_no, lib_size, rec_size, completeWidth);
}
  
int qsort_dev_point( const void *a , const void *b )
{
	FORE_DEV_DICT * p1 = (FORE_DEV_DICT*)a;
	FORE_DEV_DICT * p2 = (FORE_DEV_DICT*)b; 
 
	
	if( strcmp( p1->deviceType, p2->deviceType) > 0 ) 
		return 1;
	else if( strcmp( p1->deviceType, p2->deviceType) < 0 ) 
		return -1;
	else 
	{
		 
			if( strcmp( p1->dev_nodeID, p2->dev_nodeID ) > 0 ) 
				return 1;
			else if( strcmp( p1->dev_nodeID, p2->dev_nodeID ) < 0 ) 
				return -1;
			else return 0;
		 
	}
 
}


void printf_dev_rtdb()
{
	printf("\n\n -----------------printf_dev_rtdb--------- \n\n");

	tHandle handle = OpenLib(FORE_DEV_INFO_NO);
	if( !IsHandleValid(handle) ) {
		printf("FORE_DEV_INFO_NO is InValid \n");
		return ;
	}
	
	std::vector<FORE_DEV_DICT>  aryDev; 

	FORE_DEV_DICT rec;
	bool ret = ReadFirst(handle, (char*)&rec, sizeof(FORE_DEV_DICT ));

	while( ret )
	{   
		aryDev.push_back( rec ); 
		 
		ret = ReadNext(handle, (char*)&rec, sizeof(FORE_DEV_DICT ));
	}
	
	CloseLib( handle );
	
	if( aryDev.size() > 0 )
		qsort(aryDev.data(), aryDev.size(), sizeof(FORE_DEV_DICT), qsort_dev_point );

	for( int i=0; i<aryDev.size(); i++ )
	{
		FORE_DEV_DICT & rec = aryDev[i];
		
		char resourceID[64]={0};
		memcpy(resourceID,  rec.dky_devId, 32);
		
		printf(" dev_nodeID=%s, resourceID=%s,  status=%s, event_time=%d, manufacturerName=%s, manufacturerId=%s, model=%s, protocolType = %s, deviceType=%s reg_appName=%s \n",  rec.dev_nodeID,resourceID,  rec.status,  rec.event_time,  rec.manufacturerName,  rec.manufacturerId,  rec.model,  rec.protocolType,  rec.deviceType, rec.reg_appName);
	}

	printf(" \n \n\n dev_count = %d  \n", aryDev.size() );
	
}


int qsort_yc_point( const void *a , const void *b )
{
	FORE_YC_POINT * p1 = (FORE_YC_POINT*)a;
	FORE_YC_POINT * p2 = (FORE_YC_POINT*)b; 
 
 
	if( strcmp( p1->dev_nodeID, p2->dev_nodeID ) > 0 ) 
		return 1;
	else if( strcmp( p1->dev_nodeID, p2->dev_nodeID ) < 0 ) 
		return -1;
	else 
	{
		if( strcmp( p1->name, p2->name ) > 0 ) 
			return 1;
		else if( strcmp( p1->name, p2->name) < 0 ) 
			return -1;
		else return 0;
	}
	 
}

  
void printf_yc_rtdb( )
{
	printf("\n\n -----------------printf_yc_rtdb--------- \n\n");

	tHandle handle = OpenLib(FORE_YC_POINT_NO);
	if( !IsHandleValid(handle) ) {
		printf("FORE_YC_POINT_NO is InValid \n");
		return ;
	}
	
	std::vector<FORE_YC_POINT>  aryYc; 

	FORE_YC_POINT yc_rec;
	bool ret = ReadFirst(handle, (char*)&yc_rec, sizeof(FORE_YC_POINT ));

	while( ret )
	{   
		 
		aryYc.push_back( yc_rec );  
		 
		ret = ReadNext(handle, (char*)&yc_rec, sizeof(FORE_YC_POINT ));
	}
	
	CloseLib( handle );
	
	if( aryYc.size() > 0 )
		qsort(aryYc.data(), aryYc.size(), sizeof(FORE_YC_POINT), qsort_yc_point );

	for( int i=0; i<aryYc.size(); i++ )
	{
		FORE_YC_POINT & yc_rec = aryYc[i];
		
		printf("key=%lld, dev_nodeID=%s, val = %0.3f, appName=%s \n",  yc_rec.KEY, yc_rec.dev_nodeID, yc_rec.CurYcValue, yc_rec.appName);
	}
	 
	printf(" \n \n\n  count = %d  \n", aryYc.size() );
	
}
 

int qsort_yx_point( const void *a , const void *b )
{
	FORE_YX_POINT * p1 = (FORE_YX_POINT*)a;
	FORE_YX_POINT * p2 = (FORE_YX_POINT*)b; 
 

	if( strcmp( p1->dev_nodeID, p2->dev_nodeID ) > 0 ) 
		return 1;
	else if( strcmp( p1->dev_nodeID, p2->dev_nodeID ) < 0 ) 
		return -1;
	else 
	{
		if( strcmp( p1->name, p2->name ) > 0 ) 
			return 1;
		else if( strcmp( p1->name, p2->name ) < 0 ) 
			return -1;
		else return 0;
	}
}

void printf_yx_rtdb( )
{
	printf("\n\n -----------------printf_yx_rtdb --------- \n\n");

	tHandle handle = OpenLib(FORE_YX_POINT_NO);
	if( !IsHandleValid(handle) ) {
		printf("FORE_YX_POINT_NO is InValid \n");
		return ;
	}
	
	std::vector<FORE_YX_POINT>  aryRec; 

	FORE_YX_POINT  rec;
	bool ret = ReadFirst(handle, (char*)&rec, sizeof(FORE_YX_POINT ));

	while( ret )
	{    
		 
		 aryRec.push_back(  rec ); 

		ret = ReadNext(handle, (char*)&rec, sizeof(FORE_YX_POINT ));
	}
	
	CloseLib( handle );
	
	if( aryRec.size() > 0 )
		qsort(aryRec.data(), aryRec.size(), sizeof(FORE_YX_POINT), qsort_yx_point );

	for( int i=0; i<aryRec.size(); i++ )
	{
		FORE_YX_POINT & yx_rec = aryRec[i];
		
		printf("dev_nodeID=%s, name=%s,  time=%d,  val = %d , appName=%s \n",  yx_rec.dev_nodeID, yx_rec.name,  yx_rec.LastTime,  yx_rec.CurYxRTVal, yx_rec.appName);
	}
	 

	printf(" \n \n\n  count = %d  \n", aryRec.size() );
}
 
int qsort_yk_point( const void *a , const void *b )
{
	FORE_YK  * p1 = (FORE_YK *)a;
	FORE_YK  * p2 = (FORE_YK *)b; 
 

	if( strcmp( p1->dev_code, p2->dev_code ) > 0 ) 
		return 1;
	else if( strcmp( p1->dev_code, p2->dev_code ) < 0 ) 
		return -1;
	else 
	{
		if( strcmp( p1->fid_name, p2->fid_name ) > 0 ) 
			return 1;
		else if( strcmp( p1->fid_name, p2->fid_name ) < 0 ) 
			return -1;
		else return 0;
	}
}


void printf_yk_rtdb()
{
	printf("\n\n\-----------------printf_yk_rtdb--------- \n\n");

	tHandle handle = OpenLib(FORE_YK_NO);
	if( !IsHandleValid(handle) ) {
		printf("FORE_YK _NO is InValid \n");
		return ;
	}
	
	std::vector<FORE_YK >  aryYk; 

	FORE_YK   rec;
	bool ret = ReadFirst(handle, (char*)&rec, sizeof(FORE_YK ));

	while( ret )
	{   
		aryYk.push_back( rec ); 
		 
		ret = ReadNext(handle, (char*)&rec, sizeof(FORE_YK ));
	}
	
	CloseLib( handle );
	
	if( aryYk.size() > 0 )
		qsort(aryYk.data(), aryYk.size(), sizeof(FORE_YK ), qsort_yk_point );

	for( int i=0; i<aryYk.size(); i++ )
	{
		FORE_YK  &  rec = aryYk[i];
		
		printf("dev_code=%s, fid_name=%s, controlMode=%d, controlType=%d \n",  rec.dev_code, rec.fid_name, (int)rec.controlMode, (int)rec.controlType);
	}
}

 
int qsort_param( const void *a , const void *b )
{
	FORE_PARAM_CFG * p1 = (FORE_PARAM_CFG*)a;
	FORE_PARAM_CFG * p2 = (FORE_PARAM_CFG*)b; 
 

	if( strcmp( p1->param_name, p2->param_name ) > 0 ) 
		return 1;
	else if( strcmp( p1->param_name, p2->param_name ) < 0 ) 
		return -1;
	else return 0;
			 
}



void printf_param_rtdb()
{
	printf("\n\n\-----------------printf_param-------- \n\n");

	tHandle handle = OpenLib(FORE_PARAM_CFG_NO);
	if( !IsHandleValid(handle) ) {
		printf("FORE_PARAM_CFG_NO is InValid \n");
		return ;
	}
	
	std::vector<FORE_PARAM_CFG>  aryRec; 

	FORE_PARAM_CFG  rec;
	bool ret = ReadFirst(handle, (char*)&rec, sizeof(FORE_PARAM_CFG));

	while( ret )
	{   
		aryRec.push_back( rec ); 
		 
		ret = ReadNext(handle, (char*)&rec, sizeof(FORE_PARAM_CFG));
	}
	
	CloseLib( handle );
	
	if( aryRec.size() > 0 )
		qsort(aryRec.data(), aryRec.size(), sizeof(FORE_PARAM_CFG), qsort_param );

	for( int i=0; i<aryRec.size(); i++ )
	{
		FORE_PARAM_CFG & rec = aryRec[i];
		
		 
		printf("param_name=%s,  param_val=%s\n",   rec.param_name,  rec.param_val);  
			 
	}
}

 
void printf_devtype() 
{
	printf("\n\n\-----------------printf_devtype_rtdb--------- \n\n");

	tHandle handle = OpenLib(FORE_DEV_TYPE_NO);
	if( !IsHandleValid(handle) ) {
		printf("FORE_DEV_TYPE_NO is InValid \n");
		return ;
	}
	
	std::vector<FORE_DEV_TYPE >  aryDevType; 

	FORE_DEV_TYPE   rec;
	bool ret = ReadFirst(handle, (char*)&rec, sizeof(FORE_DEV_TYPE));

	while( ret )
	{   
		aryDevType.push_back( rec ); 
		 
		ret = ReadNext(handle, (char*)&rec, sizeof(FORE_DEV_TYPE));
	}
	
	CloseLib( handle );
	 

	for( int i=0; i<aryDevType.size(); i++ )
	{
		FORE_DEV_TYPE  &  rec = aryDevType[i];
		
		printf("id=%d, dev_type=%s, ReportingTime=%d, profilename=%s manufacturerName=%s  manufacturerId=%s model=%s protocolType=%s deviceType=%s \n", (int)rec.termtype, rec.dev_type,  rec.Reporting_interval, rec.profilename, rec.manufacturerName, rec.manufacturerId, rec.model, rec.protocolType, rec.deviceType);
	}
}
 

int qsort_mea_templ( const void *a , const void *b )
{
	FORE_MEA_TEMPLATE  * p1 = (FORE_MEA_TEMPLATE *)a;
	FORE_MEA_TEMPLATE  * p2 = (FORE_MEA_TEMPLATE *)b; 
 

	if( strcmp( p1->dev_type, p2->dev_type ) > 0 ) 
		return 1;
	else if( strcmp( p1->dev_type, p2->dev_type ) < 0 ) 
		return -1;
	else 
	{ 
		if( strcmp( p1->serviceid, p2->serviceid ) > 0 ) 
			return 1;
		else if( strcmp( p1->serviceid, p2->serviceid ) < 0 ) 
			return -1;
		else 
		{
			if( strcmp( p1->propertyName, p2->propertyName) > 0 ) 
				return 1;
			else if( strcmp( p1->propertyName, p2->propertyName ) < 0 ) 
				return -1;
			else return 0;
		}
	}
}

void printf_meatemplate()
{
	printf("\n\n\-----------------FORE_MEA_TEMPLATE-------- \n\n");

	tHandle handle = OpenLib(FORE_MEA_TEMPLATE_NO);
	if( !IsHandleValid(handle) ) {
		printf("FORE_MEA_TEMPLATE_NO is InValid \n");
		return ;
	}
	
	std::vector<FORE_MEA_TEMPLATE>  aryMeaTempl; 

	FORE_MEA_TEMPLATE   rec;
	bool ret = ReadFirst(handle, (char*)&rec, sizeof(FORE_MEA_TEMPLATE));

	while( ret )
	{   
		aryMeaTempl.push_back( rec ); 
		 
		ret = ReadNext(handle, (char*)&rec, sizeof(FORE_MEA_TEMPLATE));
	}
	
	CloseLib( handle );
	 
	if( aryMeaTempl.size() > 0 )
		qsort(aryMeaTempl.data(), aryMeaTempl.size(), sizeof(FORE_MEA_TEMPLATE), qsort_mea_templ );


	for( int i=0; i<aryMeaTempl.size(); i++ )
	{
		FORE_MEA_TEMPLATE  &  rec = aryMeaTempl[i];
		
		printf("dev_type=%s, zfType=%d, serviceid=%s, propertyName=%s, required=%d min=%s  max=%s step=%0.3f maxLength=%d method=%s curve=%d \n",  rec.dev_type, rec.zf_type, rec.serviceid,  rec.propertyName,  (int)rec.required, rec.min, rec.max, rec.step, rec.maxLength, rec.method, (int)rec.create_curve);
	}

	printf(" \n \n\n  count = %d  \n", aryMeaTempl.size() );

}

 
void remove_yc_double_name( ) 
{ 
	printf("\n\n -----------------remove yc double name--------- \n\n");  
	tHandle handle = OpenLib(FORE_YC_POINT_NO); 
	if( !IsHandleValid(handle) ) { 
		printf("FORE_YC_POINT_NO is InValid \n"); 
		return ; 
	}  
	std::map<string, long long>  mapRec;
	std::vector<FORE_YC_POINT>  aryYc;   
	FORE_YC_POINT yc_rec; 
	bool ret = ReadFirst(handle, (char*)&yc_rec, sizeof(FORE_YC_POINT ));  
	while( ret ) 
	{    
		char chrKey[128] ={0};
		sprintf(chrKey, "%s:%s", yc_rec.dev_nodeID, yc_rec.name);

		 FORE_YC_POINT * pp = (FORE_YC_POINT*)FindRec_stringkey(FORE_YC_POINT_NO, chrKey);
		 if( pp==NULL )  
		{
			printf("   find_invalid key=%lld \n ", yc_rec.KEY);
			//Delete_key(FORE_YC_POINT_NO, yc_rec.KEY);
		}
		else {
			std::map<string, long long >::iterator itMeta = mapRec.find(chrKey);
			if (itMeta == mapRec.end()) { 
				mapRec[chrKey] = yc_rec.KEY;
			}
			else {
				 long long  old_key =  itMeta->second;  

				 printf("%s: oldkey = %lld, key=%lld  ", chrKey, old_key, yc_rec.KEY);

				 
				 printf("   find_key=%lld  ", pp->KEY);

				 if( pp->KEY == old_key ) {
					printf("  Delete key=%lld  ", yc_rec.KEY);
					//Delete_key(FORE_YC_POINT_NO, yc_rec.KEY); 
				 }
				 else {
					printf("  Delete key=%lld  ", old_key);
					//Delete_key(FORE_YC_POINT_NO, old_key);
				 }
				 
				 printf(" \n ");
			}
		}

		ret = ReadNext(handle, (char*)&yc_rec, sizeof(FORE_YC_POINT )); 
	} 
	CloseLib( handle );  
}
 

void remove_dev_double_name()
{
printf("\n\n -----------------remove dev double name--------- \n\n");  
	tHandle handle = OpenLib(FORE_DEV_INFO_NO); 
	if( !IsHandleValid(handle) ) { 
		printf("FORE_DEV_INFO_NO is InValid \n"); 
		return ; 
	}  
	std::map<string, long long>  mapRec; 
	FORE_DEV_DICT rec; 
	bool ret = ReadFirst(handle, (char*)&rec, sizeof(FORE_DEV_DICT));  
	while( ret ) 
	{     
		FORE_DEV_DICT * pp = (FORE_DEV_DICT*)FindRec_stringkey(FORE_DEV_INFO_NO, rec.dev_nodeID);
		if( pp ) { 
			std::map<string, long long >::iterator itMeta = mapRec.find(rec.dev_nodeID);
			if (itMeta == mapRec.end()) { 
				mapRec[rec.dev_nodeID] =  rec.KEY;
			}
			else {
				 long long  old_key =  itMeta->second;  

				 printf("%s: oldkey = %lld, key=%lld  ", rec.dev_nodeID, old_key,  rec.KEY);
 
				 printf("   find_key=%lld  ", pp->KEY);

				 if( pp->KEY == old_key ) {
					printf("  Delete key=%lld  ", rec.KEY);
					 //Delete_key(FORE_DEV_INFO_NO,  rec.KEY); 
				 }
				 else {
					printf("  Delete key=%lld  ", old_key);
					//Delete_key(FORE_DEV_INFO_NO, old_key);
				 } 
				 
				 printf(" \n ");
			}
		}
		else {
			printf("   find_invalid key=%lld  \n ", rec.KEY);
			//Delete_key(FORE_DEV_INFO_NO, rec.KEY);
		}

		ret = ReadNext(handle, (char*)&rec, sizeof(FORE_DEV_DICT)); 
	} 
	CloseLib( handle );  
}

void update_yc_rtdb(CAnalyzeCmdLine & cmd)
{
	if( cmd.m_action != CMD_ACTION_UPDATE_DATA ) return;
						 
	char chrKey[128]={0};

	sprintf(chrKey, "%s:%s",  cmd.m_chrNodeId, cmd.m_fieldname);

	FORE_YC_POINT * pp = (FORE_YC_POINT*)FindRec_stringkey(FORE_YC_POINT_NO, chrKey);
	if( pp ) { 
		pp->CurYcValue = atof(cmd.m_val);
		pp->LastUpdateTime = time(NULL);

	}
	else {
		printf("   find_invalid key=%s  \n ", chrKey);
	}
}

void update_yx_rtdb(CAnalyzeCmdLine & cmd)
{
	 
	if( cmd.m_action != CMD_ACTION_UPDATE_DATA ) return;
						 
	char chrKey[128]={0}; 
	sprintf(chrKey, "%s:%s",  cmd.m_chrNodeId, cmd.m_fieldname); 
	FORE_YX_POINT * pp = (FORE_YX_POINT*)FindRec_stringkey(FORE_YX_POINT_NO, chrKey);
	if( pp ) {  
		pp->CurYxRTVal = atof(cmd.m_val);
		pp->LastTime = time(NULL);
	}
	else {
		printf("   find_invalid key=%s  \n ", chrKey);
	}
}


int qsort_event( const void *a , const void *b )
{
	FORE_EVENT_POINT  * p1 = (FORE_EVENT_POINT *)a;
	FORE_EVENT_POINT  * p2 = (FORE_EVENT_POINT *)b; 
 

	if(  p1->event_time > p2->event_time ) 
		return 1;
	else if( p1->event_time < p2->event_time  ) 
		return -1;
	else 
	{  
		return 0; 
	}
}

void printf_event_rtdb()
{ 
	printf("\n\n\-----------------printf_event_rtdb--------- \n\n");

	tHandle handle = OpenLib(FORE_EVENT_POINT_NO);
	if( !IsHandleValid(handle) ) {
		printf("FORE_EVENT_POINT_NO is InValid \n");
		return ;
	}
	 
	std::vector<FORE_EVENT_POINT >  aryEvent; 

	FORE_EVENT_POINT   rec;
	bool ret = ReadFirst(handle, (char*)&rec, sizeof(FORE_EVENT_POINT));
 
	while( ret )
	{   
		aryEvent.push_back( rec ); 
		 
		ret = ReadNext(handle, (char*)&rec, sizeof(FORE_EVENT_POINT));
	}
 	
	CloseLib( handle );
 	 
	if( aryEvent.size() > 0 )
		qsort(aryEvent.data(), aryEvent.size(), sizeof(FORE_EVENT_POINT), qsort_event );
 
	for( int i=0; i<aryEvent.size(); i++ )
	{
		FORE_EVENT_POINT  &  rec = aryEvent[i];
 		
		printf("id=%d, event_time=%s,  dev_nodeID=%s name=%s  pre_status=%d cur_status=%d \n",  (int)rec.event_time, base::toTimeString((int)rec.event_time).c_str(),  rec.dev_nodeID, rec.name, (int)rec.pre_status, (int)rec.cur_status );
	}
 
}



int qsort_curve( const void *a , const void *b )
{
	RTDB_YC_CURVE_5MIN * p1 = (RTDB_YC_CURVE_5MIN*)a;
	RTDB_YC_CURVE_5MIN * p2 = (RTDB_YC_CURVE_5MIN*)b; 
 
 
	if( strcmp( p1->dev_nodeID, p2->dev_nodeID ) > 0 ) 
		return 1;
	else if( strcmp( p1->dev_nodeID, p2->dev_nodeID ) < 0 ) 
		return -1;
	else 
	{
		if( strcmp( p1->name, p2->name ) > 0 ) 
			return 1;
		else if( strcmp( p1->name, p2->name) < 0 ) 
			return -1;
		else return 0;
	}
	 
}

void printf_curve_rtdb()
{
	printf("\n\n\-----------------printf_curve_rtdb--------- \n\n");

	tHandle handle = OpenLib(FORE_CURVE_NO);
	if( !IsHandleValid(handle) ) {
		printf("FORE_CURVE_NO is InValid \n");
		return ;
	}
	 
	std::vector<RTDB_YC_CURVE_5MIN>  aryCurve; 

	RTDB_YC_CURVE_5MIN   rec;
	bool ret = ReadFirst(handle, (char*)&rec, sizeof(RTDB_YC_CURVE_5MIN));
 
	while( ret )
	{   
		aryCurve.push_back( rec ); 
		 
		ret = ReadNext(handle, (char*)&rec, sizeof(RTDB_YC_CURVE_5MIN));
	}
 	
	CloseLib( handle );
 	 
	if( aryCurve.size() > 0 )
		qsort(aryCurve.data(), aryCurve.size(), sizeof(RTDB_YC_CURVE_5MIN), qsort_curve );
 
	for( int i=0; i<aryCurve.size(); i++ )
	{
		RTDB_YC_CURVE_5MIN  &  rec = aryCurve[i];
 		
		printf("key=%lld, dev_nodeID=%s, name=%s, index=%d  \n",  rec.KEY, rec.dev_nodeID, rec.name,   rec.index);
	}

	printf(" \n \n count = %d  \n", aryCurve.size() );
}
